// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MainWindow.xaml.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Visualization
{
    using System;
    using System.Windows;
    using System.Windows.Controls;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// The user interface agent that created this window
        /// </summary>
        private UserInterfaceAgent userInterfaceAgent;

        /// <summary>
        /// The name of the active view
        /// </summary>
        private string activeTabName;

        /// <summary>
        /// Initializes a new instance of the MainWindow class
        /// </summary>
        /// <param name="userInterfaceAgent">The user interface agent that created this window</param>
        public MainWindow(UserInterfaceAgent userInterfaceAgent)
        {
            this.userInterfaceAgent = userInterfaceAgent;
            this.InitializeComponent();
            this.Closed += 
                (s, e) =>
                {
                    this.userInterfaceAgent.DectivateModel(this.activeTabName);
                    this.userInterfaceAgent.OnMainWindowClosed(this);
                };
        }

        /// <summary>
        /// Adds a view. 
        /// </summary>
        /// <param name="view">The view control to add.</param>
        /// <param name="viewName">The name of the view to add.</param>
        public void AddView(FrameworkElement view, string viewName)
        {
            // add the view as a tab
            TabItem item = new TabItem();
            item.Header = viewName;
            item.Content = view;
            this.tabControl.Items.Add(item);
        }

        /// <summary>
        /// Adds a view. 
        /// </summary>
        /// <param name="view">The control to add.</param>
        /// <param name="viewName">The name of the view to add.</param>
        public void AddStatusBarControl(FrameworkElement view, string viewName)
        {
            // adds the control to the status bar
            this.statusBarPanel.Children.Add(view);
            this.userInterfaceAgent.ActivateModel(viewName, view, this);
        }

        /// <summary>
        /// Bind the view and timer data context to the model
        /// </summary>
        /// <param name="viewToBind">View to bind.</param>
        /// <param name="model">Model to be bound as data context.</param>
        public void BindViewAndTimerDataContextToModel(FrameworkElement viewToBind, Model model)
        {
            viewToBind.DataContext = model;

            // make sure something is bound to the status bar 
            if (this.MessageRate.DataContext == null)
            {
                this.MessageRate.DataContext = model;
            }
        }

        /// <summary>
        /// When the selected tab changes, the status bar needs to reflect the update rate of the selected tab.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The event args.</param>
        private void OnTabChanged(object sender, SelectionChangedEventArgs e)
        {
            // If a null reference exception is occuring here, then it is likely due to an event that
            // is being propagated from another control.  To fix this, add a handler for that event to
            // the control in question, and set e.Handled=true
            TabItem selectedTab = e.AddedItems[0] as TabItem;
            FrameworkElement view = (FrameworkElement)selectedTab.Content;

            if (this.activeTabName != null)
            {
                this.userInterfaceAgent.DectivateModel(this.activeTabName);
            }

            this.activeTabName = (string)selectedTab.Header;
            this.userInterfaceAgent.ActivateModel(this.activeTabName, selectedTab, this);
            this.MessageRate.DataContext = this.DataContext;
        }      
    }
}
